package com.itheima.edy.info.manager.dao;

import com.itheima.edy.info.manager.domain.Student;

public class StudentDao {
   static Student [] students = new Student[5];
    public boolean addStudent(Student stu) {
        //定义一个索引
        int index = -1;
        //遍历学生数组
        for (int i = 0; i < students.length; i++) {
            //把数组里的元素给了学生对象
            Student student = students[i];
            //判断对象是否为空  如果为空  把为空的索引赋值给index
            if(student == null){
                index = i;
                break;
            }
        }
        //如果不为空，返回false 说明数组里边已经满了 不能添加了
        if(index == -1){
            return false;
        }else {
            //如果为空  把传进来的学生信息传入给数组
            students[index] = stu;
            return true;
        }
    }

    public Student[] findAllStudent() {
        return students;
    }

    public void deleteStudentById(String delId) {
        //查找id在容器中所在的索引位置
        int index = getIndex(delId);
        //将该索引位置使用null元素进行替换
        students [index] = null;
    }

    public int getIndex (String id){
        int index = -1;
        for (int i = 0; i < students.length; i++) {
            Student student = students[i];
            if(students != null && student.getId().equals(id)){
                index = i;
                break;
            }
        }
        return index;
    }

    public void updateStudent(String updateId, Student newStu) {
        //查找undateId，在容器中索引位置
        int index = getIndex(updateId);
        //将该索引位置，使用新的学生对象替换
        students[index] = newStu;
    }
}
